package com.speaktoitcookbook.service;

import java.io.IOException;
import java.io.PrintWriter;

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;

import com.speaktoitcookbook.bean.Cookbook;
import com.speaktoitcookbook.dao.ICookbookDao;
import com.speaktoitcookbook.dao.factory.DaoFactory;

@WebServlet("/DailyRecommendService")
public class DailyRecommendService extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public DailyRecommendService() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json");
		
		String typeStr = request.getParameter("type");
		int type = 0;
		try{
		    type = Integer.parseInt(typeStr);
		}catch(NumberFormatException e){
		    type = 0;
		}

		ICookbookDao cookbookDao = DaoFactory.getCookbookDao();

		PrintWriter out = response.getWriter();
		List<Cookbook> cookbooklist = null;

		try {
			cookbooklist = cookbookDao.getCookbook(type, 0);// 按收藏数查询菜谱
		} catch (Exception e) {
			e.printStackTrace();
		}

		System.out.println("cookbooklist:" + cookbooklist);
		JSONArray newsjson = new JSONArray(cookbooklist, false);
		out.print(newsjson.toString());
		out.flush();
		out.close();

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}